<?php
/**
 * Created by dutuwang.net
 * User: adophper <hello@adophper.com>
 * Date: 2018/4/12
 * Time: 11:26
 */
?>
<div class="box-body">
    <p>注：最少一行数据，最多10行数据</p>
    <div class="form-inline create_data" id="audience">

    </div>
    <div class="btn-group">
        <button type="button" class="btn btn-primary" id="audience_btn">保存</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
    </div>
</div>

<script>
    $(function(){
        var audience_data = $("#audience_input").text();
        if(audience_data != '') {
            audience_data = JSON.parse(audience_data);
        }else{
            audience_data = [];
        }
        var html = _.template($("#item_tpl").html());
        if ($("#audience").find('.item_row').length == 0) {
            $("#audience").append(html({data: audience_data, sort: 0, down: false, up: false, plus: true, minus: false}));
        }
        $(document).on('click', '#audience_btn', function (e) {
            var error = 0;
            var business = [];
            $("#audience > .show").each(function (e) {
                var title = $(this).find(".title").val().replace(/(^\s*)|(\s*$)/, '');
                var num = $(this).find(".num").val();
                if (num != '' && (num < 1 || num.indexOf('.')>-1)) {
                    alert('数量只能为正整数');
                    return false;
                }
                if (title && num) {
                    error ++;
                    business.push({title: title, num: num});
                }
            })
            if (error > 0) {
                $("#audience_input").text(JSON.stringify(business));
            }
            $("#formModal").modal('hide');
        })
    })
</script>